package per.ethan.cloud.provider.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Ethan
 * @date 2020/04/09 12:04
 */
@RestController
@RefreshScope
@RequestMapping("/payment")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Value("${test.name:hello}")
    private String testName;

    @GetMapping(value = "/getPort")
    @SentinelResource(value = "getPort", blockHandler = "handlerGetPort")
    public String getPort(@RequestParam(value = "key", required = false) String key) {
        return StrUtil.format("hello: {}，testName: {}", serverPort, testName);
    }

    public String handlerGetPort(String key, BlockException e) {
        return "服务熔断";
    }

    @GetMapping(value = "/fallbackPayment")
    public String fallbackProvider() {
        int a = 10 / 0;
        return StrUtil.format("hello: {}", serverPort);
    }

}
